from sqlalchemy import select, func
from sqlalchemy.orm import joinedload
from datetime import datetime
from model.user import User, GoldCoinsLog
from db.session import get_session


async def check_user_and_password(username: str):
    """检查用户名密码是否正确

    Args:
        username (str): 用户名
    """
    async with get_session() as db:
        stmt = select(User).where(User.username == username)
        user = await db.execute(stmt)
        return user.scalars().first()


async def get_user_by_username(db, username: str):
    """通过openid获取用户

    Args:
        openid (str): openid
    """

    stmt = select(User).where(User.username == username)
    user = await db.execute(stmt)
    return user.scalars().first()


async def get_user_by_id(user_id: int):
    """通过openid获取用户

    Args:
        openid (str): openid
    """
    async with get_session() as db:
        stmt = select(User).where(User.id == user_id)
        user = await db.execute(stmt)
        return user.scalars().first()


async def create_user(db, username: str, password: str):
    """创建用户

    Args:
        openid (str): openid
    """
    user = User(username=username, password=password)
    db.add(user)
    await db.flush()
    return user


async def get_today_sign_in_log(user_id: str):
    """获取今天签到的信息

    Args:
        user_id (str): 用户id
    """
    async with get_session() as db:
        stmt = select(GoldCoinsLog).where(GoldCoinsLog.source_user_id == user_id,
                                          GoldCoinsLog.gold_coins_info_id == 2, func.date(GoldCoinsLog.create_time) == datetime.now().date())
        user = await db.execute(stmt)
        return user.scalars().first()


async def get_gold_coins_logs(user_id: int, page: int, page_size: int, gold_coins_info_id: int = None):
    """获取积分记录

    Args:
        source_user_id (int): 用户id
        page (int): 页码
        page_size (int): 每页显示
        gold_coins_info_id (int, optional): 日志id.
    """
    async with get_session() as db:

        total_stmt = select(func.count(GoldCoinsLog.id)).where(GoldCoinsLog.source_user_id == user_id,
                                                              GoldCoinsLog.is_deleted == False)
        total_result = await db.execute(total_stmt)
        total = total_result.scalar()

        if not gold_coins_info_id:
            stmt = select(GoldCoinsLog).where(GoldCoinsLog.source_user_id == user_id,
                                              GoldCoinsLog.is_deleted == False
                                              ).order_by(GoldCoinsLog.create_time.desc()
                                                         ).offset((page - 1) * page_size
                                                                  ).limit(page_size
                                                                          ).options(joinedload(GoldCoinsLog.gold_coins_info))
        else:
            stmt = select(GoldCoinsLog).where(GoldCoinsLog.source_user_id == user_id,
                                              GoldCoinsLog.is_deleted == False,
                                              GoldCoinsLog.gold_coins_info_id > gold_coins_info_id
                                              ).order_by(GoldCoinsLog.create_time.desc()
                                                         ).limit(page_size
                                                                 ).options(joinedload(GoldCoinsLog.gold_coins_info))
        gold_coins_logs = await db.execute(stmt)
        return {"total": total, "gold_coins_logs": gold_coins_logs.scalars().all()} 
